<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>viewport example - camera-controls</title>
<link rel="stylesheet" href="./style.css">
</head>

<body>
<div class="info">
	<p>
		viewport example - <a href="https://github.com/yomotsu/camera-controls">camera-controls</a><br />
	</p>
	<p>
		<label><input type="range" min="0" max="1" step="0.01" value="0" oninput="viewport.x = this.value * window.innerWidth; setViewport()"> viewport x</label><br />
		<label><input type="range" min="0" max="1" step="0.01" value="0" oninput="viewport.y = this.value * window.innerHeight; setViewport()"> viewport y</label><br />
		<label><input type="range" min="0" max="1" step="0.01" value="1" oninput="viewport.z = this.value * window.innerWidth; setViewport()"> viewport width</label><br />
		<label><input type="range" min="0" max="1" step="0.01" value="1" oninput="viewport.w = this.value * window.innerHeight; setViewport()"> viewport height</label>
	</p>
</div>

<script async src="https://unpkg.com/es-module-shims@1.8.2/dist/es-module-shims.js"></script>
<script type="importmap">
{
	"imports": {
		"three": "https://unpkg.com/three@0.161.0/build/three.module.js",
		"three/addons/": "https://unpkg.com/three@0.161.0/examples/jsm/"
	}
}
</script>
<script type="module">
import * as THREE from 'three';
import CameraControls from './dist/camera-controls.module.js';
CameraControls.install( { THREE: THREE } );
// == basics ===================================================================================

const width = window.innerWidth;
const height = window.innerHeight;
const clock = new THREE.Clock();

// == renderer =================================================================================

const renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );

// == camera ===================================================================================

const camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 100 );
camera.position.set( 0, 0, 5 );

// == scene ====================================================================================

const scene = new THREE.Scene();

const mesh = new THREE.Mesh(
	new THREE.BoxGeometry( 1, 1, 1 ),
	new THREE.MeshBasicMaterial( {
		color: 0xff0000,
		wireframe: true
	} )
);
scene.add( mesh );

const gridHelper = new THREE.GridHelper( 50, 50 );
gridHelper.position.y = - 1;
scene.add( gridHelper );

// == camera-controls ==========================================================================

CameraControls.install( {
	THREE: THREE
} );

const cameraControls = new CameraControls( camera, renderer.domElement );
cameraControls.dollyToCursor = true;

// == render ===================================================================================

function render() {

	renderer.render( scene, camera );
	console.log( 'rendered' );

}

render();

// == viewport =================================================================================

const viewport = new THREE.Vector4( 0, 0, window.innerWidth, window.innerHeight );
function setViewport() {

	camera.aspect = viewport.z / viewport.w;
	camera.updateProjectionMatrix();

	renderer.setViewport( viewport );
	cameraControls.setViewport( viewport );

	render();

}

// == animate ==================================================================================

function animate() {

	requestAnimationFrame( animate );

	const delta = clock.getDelta();
	const elapsed = clock.getElapsedTime();
	const updated = cameraControls.update( delta );

	if ( updated ) {

		render();

	}

}

animate();

// make variable available to browser console
globalThis.THREE = THREE;
globalThis.cameraControls  = cameraControls;
globalThis.viewport = viewport;
globalThis.setViewport = setViewport;
</script>
</body>
</html>
